var hidden1=Array();
var hidden2=Array();
var ownCutterArr = Array();
var allCutterArr = Array();
webix.ready(function(){
  var my_template={
    template: "<span style='font-size:20px; font-weight:bold; color:#3498DB'; >添加供应商</span>&nbsp;/&nbsp;&nbsp; SupplierCutter",
    height:45,
    borderless:true 
  };
  
 
// 显示所有供应商
var list1={
  css:"list1",
  rows:[
        {
            view:"label",
            label:"供应商:",
          },   
    {
        view:"text",
        id:"search1",
        label:"快速查找:",
        placeholder:"请输入关键字查询",
    },
    {
      view:"datatable",
      id:"supplier",
      columns:[
               {id:"supplierId",header:"供应商系统编码",fillspace:true},
               {id:"longName",header:"公司名称",fillspace:true},
               {id:"shortName",header:"公司简称",fillspace:true,editor:"text"},
               {id:"legal_person",header:"公司法人",fillspace:true,editor:"text"},
               {id:"contactor",header:"联系人名称",fillspace:true,editor:"text"},
               {id:"mobile",header:"联系人手机",fillspace:true,editor:"text"},
               {id:"telephone",header:"联系人座机",fillspace:true,editor:"text"},
               {id:"fax",header:"联系人传真",fillspace:true,editor:"text"},
               {id:"email",header:"联系人邮箱 ",fillspace:true,editor:"text"},
             ],
             height:200,
             select:true,
    }
]};
// 显示选中组的所有操作员
var list2={css:"list1",
  rows:[
  { margin:20,
    cols:[
      {
        view:"label",
        label:"已有物料:",
      },
      {
        view:"label",
        id:"num",
      }
    ]
  },
  {
      view:"text",
      id:"search3",
      label:"快速查找:",
      placeholder:"请输入关键字查询",
  },
  {
      view:"datatable",
      id:"useCutter",
      columns:[
               {id:"id",header:"序号",hidden:true,fillspace:true},
               {id:"millId",header:"物料编号",fillspace:true},
               {id:"supplierId",header:"供应商编号",hidden:true,hidden:true,fillspace:true},
               {id:"msmkLongName",header:"msmk物料名称",hidden:true,fillspace:true},
               {id:"msmkShortName",header:"msmk物料简称 ",hidden:true,fillspace:true},
               {id:"msmkMaterialCode",header:"msmk物料编码",hidden:true,fillspace:true},
               {id:"supplierMaterialCode",header:"供应商物料编码",hidden:true,fillspace:true},
               {id:"longName",header:"物料规格",fillspace:true},
               {id:"shortName",header:"物料简称",fillspace:true},
               {id:"supplierDesc",header:"供应商物料描述",fillspace:true},
               {id:"msmkDesc",header:"msmk物料描述 ",fillspace:true},
               {id:"type",header:"物料字典类型",fillspace:true,hidden:true},
               {id:"typeJson",header:"物料详细参数 ",hidden:true,fillspace:true},
               {id:"customerMaterialCode",header:"客户物料编码",fillspace:true,editor:"text"},
               {id:"customerLongName",header:"客户物料规格",fillspace:true,editor:"text"},
               {id:"customerShortName",header:"客户物料简称",fillspace:true,editor:"text"},
               {id:"safeQty",header:"安全库存 ",fillspace:true,editor:"text"},
              // {id:"opertion",header:"操作",fillspace:true,template:"<div><a href='javascript:void(0);' onclick=\"details('#millId#');\">详情</a></div>"},
             ],
             height:200,      
             editable:true,
             select:true,
    }
]};
// 显示所有操作员
var list3={css:"list1",
  rows:[
        {
            view:"label",
            label:"供应商所有刀具:",
          },   
  {
    view:"text",
    id:"search2",
    placeholder:"请输入关键字查询",
    label:"快速查找:",
  },
  {
    view:"datatable",
    height:200,
    select:true,
    multiselect:true,
    id:"allOperate",
    columns:[
             {id:"id",header:"序号",hidden:true,fillspace:true},
             {id:"millId",header:"物料编号",fillspace:true},
             {id:"supplierId",header:"供应商编号",hidden:true,fillspace:true},
             {id:"msmkLongName",header:"msmk物料名称",fillspace:true},
             {id:"msmkShortName",header:"msmk物料简称 ",fillspace:true},
             {id:"msmkMaterialCode",header:"msmk物料编码",fillspace:true},
             {id:"supplierMaterialCode",header:"供应商物料编码",fillspace:true},
             {id:"longName",header:"物料规格",fillspace:true},
             {id:"shortName",header:"物料简称",fillspace:true},
             {id:"supplierDesc",header:"供应商物料描述",fillspace:true},
             {id:"msmkDesc",header:"msmk物料描述 ",fillspace:true},
             {id:"type",header:"物料字典类型",fillspace:true,hidden:true},
             {id:"typeJson",header:"物料详细参数 ",hidden:true,fillspace:true},
            // {id:"opertion",header:"操作",fillspace:true,template:"<div><a href='javascript:void(0);' onclick=\"details('#millId#');\">详情</a></div>"},
           ],
           select:true,
           }
]};

var body={
     // margin:10,
     css:"borderGroup",
     width:1400,
     rows:[{
    	 cols:[
    	       list1]
     },
     {
    	 cols:[
    	       list2]
     },
     {css:{"text-align":"center"},margin:50,
    	 cols:[
    	       {view:"button",value:"↑",width:50,click:"addOne"},
    	       {view:"button",value:"↓",width:50,click:"clearOne"},
     	     ]
     },
     { 
    	 
    	 cols:[ 
      list3
    ]},
    {css:{"text-align":"right"},margin:10,
      cols:[
        {view:"button",value:"确定",width:60,click:"sure"}
      ]
    }
     ]
  };
  var web={
    container:"body",
    id:"mylayout",
    rows:[
      my_template,
      body
    ],
  };
  webix.ui(web);//---webix.ui结束----
// 组名搜索
$$("search1").attachEvent("onTimedKeyPress",function(){
        var value = this.getValue();
        $$("supplier").filter(function(obj){
            var boo = false;
       	 for (var key in obj) {
       if(key=="supplierId"||key=="longName"||key=="shortName"||key=="legal_person"||key!="contactor"||key!="mobile"||key!="telephone"||key!="fax"||key!="email"){
         		var boo = obj[key].toString().indexOf(value)>=0;
       		if(boo==true){
       			break;
       		}
       		 }
			}
        	 return boo;
        })
    });
// 操作员搜索
$$("search2").attachEvent("onTimedKeyPress",function(){
        var value = this.getValue();
         $$("allOperate").filter(function(obj){
             var boo = false;
        	 for (var key in obj) {
        if(key=="millId"||key=="msmkLongName"||key=="msmkShortName"||key=="msmkMaterialCode"||key!="supplierMaterialCode"||key!="longName"||key!="shortName"||key!="supplierDesc"||key!="msmkDesc"){
          		var boo = obj[key].toString().indexOf(value)>=0;
        		if(boo==true){
        			break;
        		}
        		 }
			}
         	 return boo;
        })
 });

//客户物料规格搜索
$$("search3").attachEvent("onTimedKeyPress",function(){
        var value = this.getValue();
         $$("useCutter").filter(function(obj){
        	   var boo = false;
           	 for (var key in obj) {
                 if(key=="millId"||key=="longName"||key=="shortName"||key=="supplierDesc"||key!="msmkDesc"&&key!="customerMaterialCode"||key!="customerLongName"||key!="customerShortName"||key!="safeQty"){
            	 var boo =  obj[key].toString().indexOf(value)>=0;
         		if(boo==true){
         			break;
         		}
         		 }
          	 }
          	 return boo;
        })
        
 });
//把操作员入组信息传到后台
 var i=0;
  $$('supplier').attachEvent("onBeforeSelect", function(id, selection){
	  var item = this.getItem(id).supplierId;
	  var sel = $$('supplier').getSelectedId();
	  $$("useCutter").clearAll();
      $$('useCutter').refresh();
	  $$("allOperate").clearAll();
      $$('allOperate').refresh();
	  if(sel){
        var b=getSubmitOperatorData();
      }
      hidden1[i]=b;
 	  
      submit(hidden2[i-1],hidden1[i]);
      i++;
      	var data1;
          webix.ajax().sync().post("getSupplierCutterById", "supplier_id="+item ,function(text, xml, xhr) {
      	    var postData = JSON.parse(text); 
      	  ownCutterArr = postData.data;
      	  $$("useCutter").define("data",postData.data);
          $$('useCutter').refresh();
         });
       	var data1;
        webix.ajax().sync().post("getSupplierCutterList", "supplier_id="+item ,function(text, xml, xhr) {
    	    var postData = JSON.parse(text); 
    	    allCutterArr = postData.data;
    	  $$("allOperate").define("data",postData.data);
        $$('allOperate').refresh();
       });  
  });
          var group1;
           webix.ajax().sync().post("getSupplierList", "",function(text, xml, xhr) {
        	    var postData = JSON.parse(text);
        	    $$("supplier").define("data",postData.data);
                $$('supplier').refresh();
           
           });
})

//添加一行
function addRow(){ 
	alert(1);
  var dt=$$("group");
  var data=$$("add1").getValue();
  if(data==""){
    alert("请输入要添加的组号");
  }
  else{
    var id=dt.add({
    	shortName:$$("add1").getValue()
    },0); 
     webix.ajax().sync().post("addGourp","gourp_name="+data,function(text, xml, xhr) {
   // 测试数据
    	 var retData = JSON.parse(text);
    	 if(retData.ret<0){
    		 alert(retData.msg);
    		 return;
    	 }
         webix.ajax().sync().post("getGroupList", "",function(text, xml, xhr) {
      	    var postData = JSON.parse(text); 
      	    group1=postData.data;
      	    var data = new webix.DataCollection({data:group1});
      	    $$('group').data.sync(data);
         });
 });
    var b=getSubmitOperatorData();
    submit(id,b);
  }
};
function clearAll(){
  $$('useCutter').clearAll();
  $$("num").setValue($$("useCutter").count());
}
//添加所有操作员
function addAll(){
var sel = $$('supplier').getSelectedId();
  if(sel){
    var item=Array();
    for(var i=0;i<$$("allOperate").count();i++){
      item[i]=$$('allOperate').getItem($$("allOperate").getIdByIndex(i));
    }
    common(item);
  }
  else{
    alert("请先选择供应商");
  }
}

function common(item){
	var dt=$$("useCutter");
	  var a=Array();
	  var b=Array();
	  var c = $$("useCutter").count();
	  var flag2=Array();
	  for(var j=0;j<item.length;j++){
	      flag2[j]=true;
	      for(var i=0;i<c;i++){
	      a[i]=$$("useCutter").getIdByIndex(i);
	      b[i]=$$('useCutter').getItem(a[i]);
	        if(b[i].millId==item[j].millId){
	          flag2[j]=false;
	          alert(b[i].millId+"已经在该组中");
	        }
	      }
	    }
	    for(var i=0;i<item.length;i++){ 
	     if(flag2[i]){
	    	 dt.add(item[i],i);
	    	 dt.eachRow(function(row){
	 				if(dt.getItem(row).millId==item[i].millId&&dt.getItem(row).supplierId==item[i].supplierId){
	 					  dt.editRow(row);
	 					  dt.addRowCss(row, "newRow2");
	  					}    
	 		  });
	      }
	    }
	  $$("num").setValue($$("useCutter").count());
	} 

function addOne(){
	var sel = $$('supplier').getSelectedId();
	if(sel){  
	  var item=Array();
	  var dt=$$("useCutter");
	  var id = $$('allOperate').getSelectedId(); 
	  //目前操作员的数量
	  var c = dt.count();
	  var a=Array();
	  var b=Array();
	  var flag1=true;
	  var flag2=Array();
	    var aaa=$$('allOperate').getItem(id);
	     for(var i=0;i<c;i++){
	      a[i]=$$("useCutter").getIdByIndex(i);
	      b[i]=$$('useCutter').getItem(a[i]);
	      if(b[i].millId==aaa.millId){
	        flag1=false;
	        alert(aaa.millId+"刀具已绑定");
	      }
	    }
	    if(flag1){
	      dt.add(aaa,0);
		  dt.eachRow(function(row){
				if(dt.getItem(row).millId==aaa.millId&&dt.getItem(row).supplierId==aaa.supplierId){
					  dt.editRow(row);
					  dt.addRowCss(row, "newRow2");
 					}    
		  });
	    }
	    $$("num").setValue($$("useCutter").count());
	  }
	  else{
	    alert("请先选择供应商");
	  }
	}
function clearOne(){
  var id = $$('useCutter').getSelectedId(); 
  $$('useCutter').remove(id);
  $$("num").setValue($$("useCutter").count());
}
function sure(){
  var id = $$('supplier').getSelectedId();
  //获取当时的组名 
  var item1 = $$('supplier').getItem(id).supplierId;
   // 获取对应的操作员
 var boo = true
 var mills = ""
//  $$("useCutter").filter(function(obj){
//    if(obj.hasOwnProperty("customerLongName")&&obj.customerLongName!=''&&obj.hasOwnProperty("customerMaterialCode")&&obj.customerMaterialCode!=''&&obj.hasOwnProperty("customerShortName")&&obj.customerShortName!=''&&obj.hasOwnProperty("safeQty")&&obj.safeQty!=''){    	  
//	  return obj.customerLongName.indexOf("")>=0;
//      }else{
//    	  mills+=obj.millId+","
//     	  boo =false
//    }
// })
  var dt = $$('useCutter')
  var b=getSubmitOperatorData();
  for ( var i in b) {
    if(b[i].hasOwnProperty("customerLongName")&&b[i].customerLongName!=''&&b[i].hasOwnProperty("customerMaterialCode")&&b[i].customerMaterialCode!=''&&b[i].hasOwnProperty("customerShortName")&&b[i].customerShortName!=''){    	  
    	
    }else{
    	  mills+=b[i].millId+","
     	  boo =false
    }
  }
 if(!boo){
	  alert("请填写系统物料编码："+mills.substring(0,mills.length-1)+"客户物料信息");
	  return false
 }
  console.log(b)
  var params={};
  params.supplier_id=item1;
  params.cutters=b;
  //将数据传到后台
  dt.editStop();
    webix.ajax().sync().post("submitSupplierCutter",params,function(text, xml, xhr) {
    		alert("执行成功");
    		location.reload();
   });
}
function submit(id,b){
  var sel = $$('supplier').getSelectedId(); 
  if(sel){
    if(id!=sel){
     //获取当时的组名 
      var item = $$('supplier').getItem(sel).gourp_id;
      var params={};
	  params.gourp_id=item;
	  params.operator_ids=b;
   //将数据传到后台
//    webix.ajax().post("editGourpOperatorRef",params,function(text, xml, xhr) {
//   });
    }
  }
}
function getSubmitOperatorData(){
	var c = $$("useCutter").count();
	var jsonArr = new Array();
	  var str= "";
	  for(var i=0;i<c;i++){
			var operator = $$('useCutter').getItem($$("useCutter").getIdByIndex(i));
			jsonArr.push(operator);
 	  }
	  return jsonArr;
	
}
function details(millId){
	　　for(var i=0;i<ownCutterArr.length;i++){ 
		if(millId==ownCutterArr[i].millId){
			
		}
		
	}
}